using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RAG.Domain.Configuration;
using RAG.Domain.Repositories;
using RAG.Domain.Services;
using RAG.Infrastructure.Data;
using RAG.Infrastructure.Repositories;
using RAG.Infrastructure.Services;

namespace RAG.Infrastructure;

public static class ServiceCollectionExtension
{
    /// <summary>
    /// 添加基础设施层所有服务
    /// </summary>
    public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
    {
        // 配置PostgreSQL数据库连接
        services.AddDbContext<RagAIDbContext>(options =>
            options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"),
                npgsqlOptions => npgsqlOptions.UseVector()));

        // 配置设置
        services.Configure<QwenApiSettings>(configuration.GetSection("QwenApi"));
        services.Configure<DeepSeekApiSettings>(configuration.GetSection("DeepSeekApi"));
        services.Configure<RAGSettings>(configuration.GetSection("RAGSettings"));

        // HTTP客户端
        services.AddHttpClient<QwenEmbeddingService>();
        services.AddHttpClient<DeepSeekChatService>();

        // 领域服务
        services.AddScoped<IEmbeddingService, QwenEmbeddingService>(); // 启用真实的embedding服务
        // services.AddScoped<IEmbeddingService, MockEmbeddingService>(); // 临时禁用测试服务
        services.AddScoped<IChatService, DeepSeekChatService>();
        services.AddScoped<ITextChunkingService, TextChunkingService>();
        services.AddScoped<IVectorSearchService, VectorSearchService>();

        // 仓储
        services.AddScoped<IDocumentRepository, DocumentRepository>();
        services.AddScoped<IDocumentChunkRepository, DocumentChunkRepository>();
        // services.AddScoped<IVectorRepository, VectorRepository>(); // 暂时禁用，使用KnowledgeBase替代
        services.AddScoped<IKnowledgeBaseRepository, KnowledgeBaseRepository>();
        services.AddScoped<IUserRepository, UserRepository>();

        // 注册通用仓储
        services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));

        // 文件存储服务
        services.AddScoped<IFileStorage, LocalFileStorage>();

        // 数据初始化服务
        services.AddScoped<DataSeederService>();

        return services;
    }
}